// pages/home/subscribe/com/subscribeWrapper/subscribeWrapper.js
import api from "../../common/api";
import {
  dialog
} from '../../component/userStatusDialog/control'
import * as user from '../../models/user'

Component({
  /**
   * 组件的配置
   */
  options: {
    styleIsolation: 'isolated',
    pureDataPattern: /^_/,
  },
  /**
   * 组件的属性列表
   */
  properties: {
    jump: {
      type: String,
      value: '',
    },
    subscribeId: {
      type: String,
      value: '',
    },
    redirectTo: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    _isRefreshFollow: false,
    _jumpOptions: {
      estate: '/pages/addHouse/chooseHouse/chooseHouse?subscription=true&pagefrom=subscription&',
      area: '/pages/home/subscribe/tradingArea/tradingArea?'
    }
  },
  /**
   * 组件的生命周期
   */
  lifetimes: {
    attached: function () {},
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  /**
   * 页面的生命周期
   */
  pageLifetimes: {
    show: function () {
      this.refreshOfficialAccount()
    },
    hide: function () {
      // 页面被隐藏
    },
    resize: function (size) {
      // 页面尺寸变化
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {

    checkOfficialAccount() {
      return new Promise((resolve, reject) => {
        user.getOfficialAccount()
          .then(res => {
            if (res === '1') {
              resolve()
            } else {
              reject()
            }
          })
          .catch(() => {
            wx.showToast({
              icon: 'none',
              title: '网络错误，请稍后再试~',
            })
          })
      })
    },

    onClickBtn() {
      this.checkOfficialAccount()
        .then(() => {
          this.handleResult()
        })
        .catch(() => {
          dialog({
            dialog: 'public1',
            confirm: () => {
              this.data._isRefreshFollow = true
              let url = 'https://mp.weixin.qq.com/s/I8sdEESdeHIoKLyYVnDcYQ'
              wx.navigateTo({
                url: `/pages/webView/webView?webViewUrl=${url}`
              })
            }
          })
        })
    },

    handleResult() {
      const {
        jump,
        subscribeId,
        _jumpOptions,
      } = this.data
      if (jump) {
        this.jumpSubscribe(_jumpOptions[jump])
      }
      if (subscribeId) {
        this.subscribe(subscribeId)
      }
    },

    jumpSubscribe(url) {
      const {
        redirectTo
      } = this.data
      this.triggerEvent("beforeJump")
      wx[redirectTo ? 'redirectTo' : 'navigateTo']({
        url,
      })
    },

    subscribe(hdResblockId) {
      return new Promise((resolve, reject) => {
        wx.showLoading({
          title: '订阅中...',
        })
        api.subscribe({
          hdResblockId,
          stated: 0,
        }, res => {
          wx.hideLoading()
          if (res && res.data && res.data.isSuccess) {
            this.triggerEvent('subscribeSuccess')
            resolve()
          } else {
            this.triggerEvent('subscribeFail')
            reject()
          }
        }, () => {
          wx.hideLoading()
          this.triggerEvent('subscribeFail')
          reject()
        })
      })
    },

    refreshOfficialAccount() {
      if (this.data._isRefreshFollow) {
        this.data._isRefreshFollow = false
        user.updateOfficialAccount()
      }
    },

  }
})